package ;


#if macro
import scuts.Core;
import scuts.ListMake;

using scuts.Core;
import scuts.F;

import haxe.macro.Context;
import haxe.macro.Expr;
import scuts.macro.Convert;
import scuts.macro.Make;

using scuts.macro.Parse;
using scuts.macro.Print;

#end


#if !macro extern #end 
class PrinterMacro 
{

	@:macro public static function showVarsExpr(val1:Expr, val2:Expr) 
	{
		var expr = "{ var a = ${0}; var b = ${1}; }".parse([val1, val2]);
		var s = expr.exprStr();
		trace(s);
		return Make.mkBlock([]);
	}
	
	@:macro public static function showVarsExpr2(val1:Expr, val2:Expr) 
	{
		var expr = "{ var a = $0; var b = $1; }".parse([val1, val2]);
		var s = expr.exprStr();
		trace(s);
		return Make.mkBlock([]);
	}
	
	@:macro public static function showVarsExpr3(val1:Expr, val2:Expr) 
	{
		var expr = "{ var a = $first; var b = $second; }".parse({first:val1, second:val2});
		var s = expr.exprStr();
		trace(s);
		return Make.mkBlock([]);
	}
	
	@:macro public static function showSimpleExpr(val1:Expr) 
	{
		var expr = "${0}".parse([val1]);
		
		var s = expr.exprStr();
		trace(s);
		return Make.mkBlock([]);
	}
	
	@:macro public static function showWithType(val:Expr) 
	{
		var expr = "{ var a:$0 = $1;}".parse([Context.typeof(val), val]);
		
		var s = expr.exprStr();
		trace(s);
		return Make.mkBlock([]);
	}
	@:macro public static function funcWithReturn(val:Expr) 
	{
		var expr = "function ():$0 return $1".parse([Context.typeof(val), val]);
		
		var s = expr.exprStr();
		trace(s);
		return Make.mkBlock([]);
	}
	
	@:macro public static function funcWithReturnAndArg(arg:Expr, ret:Expr) 
	{
		var expr = "function (a:$0):$1 return $2".parse([Context.typeof(arg), Context.typeof(ret), ret]);
		
		var s = expr.exprStr();
		trace(s);
		return Make.mkBlock([]);
	}
	
	@:macro public static function tryCatch(catchType:Expr) 
	{
		var expr = "try bla() catch (e:$0) null".parse([Convert.exprToType(catchType)]);
		
		var s = expr.exprStr();
		trace(s);
		return Make.mkBlock([]);
	}
	
	@:macro public static function test(expr:Expr) 
	{
		trace(Convert.exprToType(expr));
		return Make.mkBlock([]);
	}
	
}

class Main
{
	static function main () {
		
		/*
		PrinterMacro.showSimpleExpr(1);
		PrinterMacro.showVarsExpr("500", 2);
		PrinterMacro.showVarsExpr2("foo 1", 4);
		PrinterMacro.showVarsExpr3("foo 3", 6);
		
		PrinterMacro.showVarsExpr3( { a : "hi" }, 6);
		
		PrinterMacro.showWithType( function () return 1 );
		
		PrinterMacro.funcWithReturn( function () return 1 );
		
		PrinterMacro.funcWithReturnAndArg(1, "hello");
		
		
		*/
		PrinterMacro.tryCatch(String);
		PrinterMacro.test(Array);
	}
}
